const router = require('koa-router')();
const query = require('../utils/query');

router.prefix('/article');

router.post('/v1/add', async (ctx) => {
  let content = ctx.request.body.myfile;
  const data = {
    content,
    createdAt: Date.now(),
  };
  const result = await query('INSERT INTO pb_article SET ?', [data]);
  if (result.affectedRows == 1) {
    ctx.body = '1';
  } else {
    ctx.body = '0';
  }
});

router.get('/v1/get', async (ctx) => {
  const result = await query(
    'SELECT id,content,createdAt,follow FROM pb_article ORDER BY follow DESC'
  );
  ctx.body = result;
});

module.exports = router;
